﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Common.Configuration;
using ForzaGT3.Areas.Admin.Models;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class DriverController : Controller
    {
        //
        // GET: /Admin/Driver/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Name", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Number", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Driver> data = Driver.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Driver";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(DriverModel model)
        {
            
            if (ModelState.IsValid)
            {
                Driver data = new Driver();
                data.DriverId = Guid.NewGuid();
                data.Code = model.Code;
                data.Name = model.Name;
                data.Remarks = model.Remarks;
                data.Number = model.Number;
                data.Points = model.Points;
                data.CarName = model.CarName;
                data.TeamId = model.TeamId;
                data.CountryId = model.CountryId;

                if (model.ProfileImage != null)
                {
                    model.ProfileImage.SaveAs(Server.MapPath("~" + SiteSettings.DriversImageFolder) + model.ProfileImage.FileName);
                    data.ProfilePictureUrl = SiteSettings.DriversImageFolder + model.ProfileImage.FileName;
                }

                if (model.CarImage != null)
                {
                    model.CarImage.SaveAs(Server.MapPath("~" + SiteSettings.CarsImageFolder) + model.CarImage.FileName);
                    data.CarPhotoUrl = SiteSettings.CarsImageFolder + model.CarImage.FileName;
                }

                data.CreatedBy = HttpContext.User.Identity.Name;
                data.CreatedDate = DateTime.Now;
                data.Insert();

                return RedirectToAction(MVC.Admin.Driver.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            Driver data = Driver.GetById(Id);
            DriverModel model = new DriverModel();
            model.Code = data.Code;
            model.Name = data.Name;
            model.Number = data.Number;
            model.Points = data.Points;
            model.Remarks = data.Remarks;
            model.ProfileImageUrl = data.ProfilePictureUrl;
            model.CarImageUrl = data.CarPhotoUrl;
            if (data.CountryId != null)
            {
                model.CountryId = data.CountryId.Value;
            }
            model.TeamId = data.TeamId;
            model.CarName = data.CarName;
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(DriverModel model)
        {
            Driver data = Driver.GetById(model.Id);
            if (data != null)
            {
                data.Code = model.Code;
                data.Name = model.Name;
                data.Remarks = model.Remarks;
                data.Number = model.Number;
                data.Points = model.Points;
                data.CarName = model.CarName;
                data.TeamId = model.TeamId;
                data.CountryId = model.CountryId;

                if (model.ProfileImage != null)
                {
                    model.ProfileImage.SaveAs(Server.MapPath("~" + SiteSettings.DriversImageFolder) + model.ProfileImage.FileName);
                    data.ProfilePictureUrl = SiteSettings.DriversImageFolder + model.ProfileImage.FileName;
                }

                if (model.CarImage != null)
                {
                    model.CarImage.SaveAs(Server.MapPath("~" + SiteSettings.CarsImageFolder) + model.CarImage.FileName);
                    data.CarPhotoUrl = SiteSettings.CarsImageFolder + model.CarImage.FileName;
                }

                data.ModifiedBy = HttpContext.User.Identity.Name;
                data.ModifiedDate = DateTime.Now;
                data.Update();
                return RedirectToAction(MVC.Admin.Driver.Index());
            }

            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Driver data = Driver.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }

            return RedirectToAction(MVC.Admin.Driver.Index());
        }
    }
}
